package com.egdigital.allzic.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;
import com.android.volley.toolbox.Volley;
import com.egdigital.allzic.ApplicationGlobal;
import com.egdigital.allzic.service.DownloadState;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

public class BaseMainFragment extends SherlockFragment {

	private static final String	TAG				= BaseMainFragment.class.getSimpleName();
	protected DownloadState		mDownloadState	= DownloadState.LOADING;
	// separate id for includes
	protected RelativeLayout	mLoadingView;
	protected LinearLayout		mReloadView;
	protected TextView			mNoResultView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setmDownloadState(DownloadState.DEFAULT);
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		view.setOnTouchListener(new View.OnTouchListener() { // click through fragment issue trick solves ;)

			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
		super.onViewCreated(view, savedInstanceState);
	}

	/**
	 * Update {@link Volley} download state
	 * 
	 * @param mDownloadState
	 *        {@link DownloadState}
	 */
	protected void setmDownloadState(DownloadState mDownloadState) {
		this.mDownloadState = mDownloadState;
	}

	/**
	 * crossfade mLoadingView and mReloadView OR just FadeIn mLoadingView
	 */
	protected void setLoading(View view) {
		Log.i(TAG, "setLoading : " + mDownloadState.toString());
		mReloadView.setVisibility(View.GONE);
		view.setVisibility(View.GONE);
		mNoResultView.setVisibility(View.GONE);
		mLoadingView.setVisibility(View.VISIBLE);
		setmDownloadState(DownloadState.LOADING);
	}

	/**
	 * crossfade any {@link View} and mReloadView
	 * 
	 * @param listener
	 */
	protected void setReload(View view) {
		Log.w(TAG, "setReload : " + mDownloadState.toString());
		view.setVisibility(View.GONE);
		mLoadingView.setVisibility(View.GONE);
		mNoResultView.setVisibility(View.GONE);
		// Utils.fadeInView(mReloadView, Utils.ANIMATION_DURATION_FAST);
		mReloadView.setVisibility(View.VISIBLE);
		setmDownloadState(DownloadState.RETRY);
	}

	/**
	 * crossfade the mLoadingView and the {@link View} to display
	 * 
	 * @param view
	 */
	protected void setLoaded(View view) {
		Log.i(TAG, "setLoaded : " + mDownloadState.toString());
		mReloadView.setVisibility(View.GONE);
		mNoResultView.setVisibility(View.GONE);
		// Utils.fadeInView(view, Utils.ANIMATION_DURATION_FAST);
		mLoadingView.setVisibility(View.GONE);
		view.setVisibility(View.VISIBLE);
		setmDownloadState(DownloadState.LOADED);
	}

	/**
	 * crossfade the mLoadingView and the mNoResultView
	 * 
	 * @param view
	 */
	protected void setNoResult(View view) {
		Log.i(TAG, "setNoResult : " + mDownloadState.toString());
		mReloadView.setVisibility(View.GONE);
		mLoadingView.setVisibility(View.GONE);
		// Utils.fadeInView(mNoResultView, Utils.ANIMATION_DURATION_FAST);
		view.setVisibility(View.GONE);
		mNoResultView.setVisibility(View.VISIBLE);
		setmDownloadState(DownloadState.LOADED);
	}

	/**
	 * Send an event to GoogleAnalytics server
	 * 
	 * @param context
	 *        the application context
	 * @param action
	 * @param category
	 * @param label
	 * @param value
	 */
	public static void analyticsSendEvent(String name) {
		// Get tracker
		Tracker tracker = ((ApplicationGlobal) ApplicationGlobal.getContext()).getTracker();
		// Set screen name
		tracker.setScreenName(name);
		tracker.send(new HitBuilders.AppViewBuilder().build());
	}
}
